package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.Permission;
import cn.wolfcode.crud.mapper.PermissionMapper;
import cn.wolfcode.crud.qo.PageResult;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.IPermissionService;
import cn.wolfcode.crud.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }


    @Transactional(readOnly = true)
    public Permission get(Long id) {

        return  permissionMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<Permission> listAll() {

        return  permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
       List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }
    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {

        //获取数据库中所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //从项目对象中调用根据注解来拿所有贴了注解的对象
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //循环对象从里面拿mapper的value得到controller对象们
        for (Object controller:beans.values()) {
            //拿到controller的字节码对象,因为是cglib动态代理,子类没有方法,所以去拿真实的父类
            Class clazz = controller.getClass().getSuperclass();
            //获取所有的controller的方法
            Method[] methods = clazz.getDeclaredMethods();
            //循环所有的方法
            for (Method method : methods) {
                //判断方法是否有贴权限注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if(annotation!=null){

                    //获取权限表达式方法1

                    String expression = annotation.value()[0];
                   /* //拿到类名
                    String simpleName = clazz.getSimpleName();
                    //替换类名后面的Controller
                    simpleName = simpleName.replace("Controller", "");
                    //把类名变成小写
                    simpleName = StringUtils.uncapitalize(simpleName);
                    //拿到方法名
                    String methodName = method.getName();
                    //拿到权限表达式
                    String expression = simpleName+":"+methodName;*/

                    //判断数据库是否存在该表达式,如果不包含才加入
                    if(!expressions.contains(expression)){
                        //表示有贴注解,需要处理,拿到注解里的name值
                        String name = annotation.value()[1];
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }

                }

            }


        }

        

    }

    @Override
    public List<String> selectByEmpId(Long id) {

        return permissionMapper.selectByEmpId( id);
    }
}
